package com.lbfamous.regex;

//import java.util.regex.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class ReplaceDemo {
    
    public static void main(String[] args) {
        
        String patt = "\\bfavor\\b";

        String input = "Do me favor? Fetch my favorite.";
        System.out.println("Input: " + input);

        Pattern r = Pattern.compile(patt);
        Matcher m = r.matcher(input);

        System.out.println("ReplaceAll: " + m.replaceAll("favour"));
        m.reset();

        System.out.println("Append methods");

        StringBuffer sb = new StringBuffer();
        while(m.find()){
            m.appendReplacement(sb,"favour");
        }
        m.appendTail(sb);

        System.out.println(sb.toString());
    }
}